<?php

/**
 * MVM_MALL 网上商店系统 支付处理结果
 * ============================================================================
 * 版权所有 (C) 2007-2010 www.mvmmall.com，并保留所有权利。
 * 网站地址: http://www.mvmmall.com
 * ----------------------------------------------------------------------------
 * 这是一个免费开源的软件；这意味着您可以在不用于商业目的的前提下对程序代码
 * 进行修改、使用和再发布。
 * ============================================================================
 * $Author:  www.mvmmall.com  $
 * $Date: 2008-07-01 $
 * $Id: respond.php  www.mvmmall.com$
 * ---------------------------------------------
*/

require_once 'include/common.inc.php';

/**为空表示云网的支付类**/
if (empty($code)){
   $code='cnpay'; 
}else {
  $code = dhtmlchars($code);  
}
$pay = $db->get_one("SELECT class_name,cfg FROM `{$tablepre}payment_table` WHERE class_name='$code'");
if(!$pay){
    show_msg('pass_worng','close');
} else {
    require_once 'include/payment/'. $pay['class_name'].'.class.php';
}
$rowst = new  $pay['class_name'](unserialize($pay['cfg']));
if($rowst->pay_receive()) {
	show_msg('pay_success',MVMMALL_DIR);
} else {
	show_msg('pay_fail');
}

function change_order($sn){
	/**读支付记录表**/
	$pay_log = $GLOBALS['db']->get_one("SELECT * FROM `{$GLOBALS['tablepre']}pay_log` WHERE order_sn='$sn'");
	//如果status==1，表示已经支付过了.
	$pay_log['status']==1 && show_msg('pay_again',MVMMALL_DIR);

	//如果order_type==1，表示是预存款
	if($pay_log['order_type']==1){
		$money_log = $GLOBALS['db']->get_one("SELECT uid,money_sess,money_id FROM `{$GLOBALS['tablepre']}money_table` WHERE money_sess='$sn'");
		//更新预存款状态
		$GLOBALS['db']->query("UPDATE `{$GLOBALS['tablepre']}money_table` SET  approval_date = '$GLOBALS[m_now_time]' WHERE uid='$money_log[uid]'");
		//更新会员预存款内容信息,预存款状态
		$GLOBALS['db']->query("UPDATE `{$GLOBALS['tablepre']}member_table` SET  member_money = member_money + '$pay_log[order_amount]' WHERE member_id = '$money_log[money_id]'");
		//取回会员预付款状态
		$rt_user = $GLOBALS['db']->get_one("SELECT member_money,member_id FROM `{$GLOBALS['tablepre']}member_table` WHERE member_id = '$money_log[money_id]'");
		//更新session状态
		$_SESSION['user']['mvm_sess_money'] = $rt_user['member_money'];
	}

	else {
		//定单信息
		$order_info = $GLOBALS['db']->get_one("SELECT ordersn,uid,buy_mobile,email,ordersn,order_amount,pay_name FROM `{$GLOBALS['tablepre']}order_info` WHERE ordersn='$sn'");
		//发送邮件
		if ($GLOBALS['mm_mail_receipt']==1){
			$order_info['order_amount']  = currency($order_info['order_amount']);
			include_once template('email_pay');
			$mail_content = str_replace(array('<!--<!---->','<!---->',"\r",substr(MVMMALL_ROOT,0,-1)),'',ob_get_contents());
			ob_end_clean();
			smtp_mail($order_info['email'],$GLOBALS['mm_mall_name'].$GLOBALS['lang']['buy_payment'],$mail_content,html);
			unset($mail_content);
		}
		//送积分,判断是否开通
		if($GLOBALS['mm_point_method']==2  && $GLOBALS['mm_point_use']==1 && $GLOBALS['m_check_uid']){
			//获得商品积分统计信息
			$result = $GLOBALS['db']->query("SELECT a.goods_id,a.order_id,a.buy_number,b.uid AS goods_id,b.goods_point FROM `{$GLOBALS['tablepre']}order_goods` a LEFT JOIN `{$GLOBALS['tablepre']}goods_table` b ON a.goods_id=b.uid WHERE a.order_id='$order_info[uid]'");
			while ($list=$GLOBALS['db']->fetch_array($result)) {
				$point += $list['goods_point'] * $list['buy_number'];
				//扣掉库存
				if ($GLOBALS['mm_stock_use']==1 && $GLOBALS['mm_stock_method']){
					$GLOBALS['db']->query("UPDATE `{$GLOBALS['tablepre']}goods_table` SET goods_stock  = goods_stock - $list[buy_number] WHERE uid = '$list[goods_id]'");
				}
			}

			$user_point = $GLOBALS['db']->get_one("SELECT `point_sess`,`point_category` FROM `{$GLOBALS['tablepre']}point_table` WHERE `point_category`='1' AND `point_sess` = '$sn'");//记录表
			//加积分
			if($user_point==false && $point>0){
				$query = "INSERT INTO `{$GLOBALS['tablepre']}point_table` SET
                      point_id       = '$GLOBALS[m_check_id]',
                      point_sess     = '$sn',
                      point_add      = '$point',
                      point_reason   = '$order_info[ordersn]',
                      modify_id      = '$GLOBALS[m_check_id]',
                      modify_ip      = '$GLOBALS[m_user_ip]',
                      point_category = '1',
                      register_date  = '$GLOBALS[m_now_time]',
                      approval_date  = '$GLOBALS[m_now_time]' ";
				$GLOBALS['db']->query($query);
				//更新会员数据库积分
				$GLOBALS['db']->query("UPDATE `{$GLOBALS['tablepre']}member_table` set member_point = member_point+'$point' WHERE uid = '$GLOBALS[m_check_uid]'");
				//更新session,取会员积分
				$member = $GLOBALS['db']->get_one("SELECT member_point,uid FROM `{$GLOBALS['tablepre']}member_table` WHERE uid = '$GLOBALS[m_check_uid]'");
				$_SESSION['user']['mvm_sess_point']=$member['member_point'];

			}
			//end
		}
		//结算确认发送短信
		if($GLOBALS['mm_sms_use'] == '1' && $GLOBALS['mm_sms_receipt']=='1' && $order_info['buy_mobile']!=''){
			sms_send($order_info['buy_mobile'], "$GLOBALS[mm_sms_message3]");
		}
		$GLOBALS['db']->query("UPDATE `{$GLOBALS['tablepre']}order_info` SET status='2' WHERE ordersn = '$sn'");
	}
	//更新支付记录状态
	$GLOBALS['db']->query("UPDATE `{$GLOBALS['tablepre']}pay_log`  SET  status = '1' WHERE log_id='$pay_log[log_id]'");
}
//系统定单信息
function order_info($sn){
    return $GLOBALS['db']->get_one("SELECT * FROM `{$GLOBALS['tablepre']}pay_log` WHERE order_sn = '$sn'");
}